package com.example.userdemo.controller;

import com.example.userdemo.model.Project;
import com.example.userdemo.service.ProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/api/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping
    public ResponseEntity<Page<Project>> getAllProjects(
            @RequestParam(required = false) String customerName,
            @RequestParam(required = false) String projectName,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startDate,
            @RequestParam(required = false) String workArea,
            @RequestParam(required = false) Integer projectStatus,
            @RequestParam(required = false) String wellCount,
            @RequestParam(required = false) String wellDepth,
            @RequestParam(required = false) String projectCode,
            @RequestParam(required = false) BigDecimal amount,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        if (customerName != null) {
            wrapper.like("customer_name", customerName);
        }
        if (projectName != null) {
            wrapper.like("project_name", projectName);
        }
        if (startDate != null) {
            wrapper.eq("start_date", startDate);
        }
        if (workArea != null) {
            wrapper.like("work_area", workArea);
        }
        if (projectStatus != null) {
            wrapper.eq("project_status", projectStatus);
        }
        if (wellCount != null) {
            wrapper.like("well_count", wellCount);
        }
        if (wellDepth != null) {
            wrapper.like("well_depth", wellDepth);
        }
        if (projectCode != null) {
            wrapper.like("project_code", projectCode);
        }
        if (amount != null) {
            wrapper.eq("amount", amount);
        }
        Page<Project> projects = projectService.getAllProjects(new Page<>(page, pageSize), wrapper);
        return new ResponseEntity<>(projects, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Project> getProjectById(@PathVariable(value = "id") Integer projectId) {
        Project projectObj = projectService.getProjectById(projectId);
        Optional<Project> project = Optional.ofNullable(projectObj);
        return project.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<Project> createProject(@RequestBody Project project) {
        Project createdProject = projectService.createProject(project);
        return new ResponseEntity<>(createdProject, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Project> updateProject(@PathVariable(value = "id") Integer projectId,
                                                 @RequestBody Project projectDetails) {
        projectDetails.setId(projectId);
        Project updatedProject = projectService.updateProject(projectDetails);
        return new ResponseEntity<>(updatedProject, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProject(@PathVariable(value = "id") Integer projectId) {
        projectService.deleteProject(projectId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
} 